﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Card.Make.Pro.Application.Dapper;
using DevExpress.XtraCharts;

namespace Card.Make.Pro.Form
{
    public partial class FrmHome : DevExpress.XtraEditors.XtraForm
    {
        public FrmHome()
        {
            InitializeComponent();

            _dapperClient = new DapperClient(new Config() { ConnectionString = Properties.Settings.Default.ConnectionString, DbType = DbStoreType.SqlServer });
        }


        /// <summary>
        /// dapper client
        /// </summary>
        private readonly DapperClient _dapperClient;



        private void CreateChart()
        {
            
            List<int> yData = new List<int>();
            var data = _dapperClient.ExecuteDataTable(
                  "SELECT(SELECT COUNT(1) FROM Card_Table WHERE IFPrinted = '0') AS '未打印',(SELECT COUNT(1) FROM Card_Table WHERE IFPrinted = '1') AS '已打印',(SELECT COUNT(1) FROM Card_Table) AS '总量',(select count(1) from Card_Table where CardID is not null) as '已绑卡',(select count(1)from Card_Table where [IFGranted] = 1) as '已发卡',(select count(1) from Card_Table where DATEDIFF(day,[GrantedDT], GETDATE()) = 0)as '当天发放'");
            if (data.Rows.Count == 0)
            {
                return;
            }
            yData.Add((int)data.Rows[0]["已打印"]);
            yData.Add((int)data.Rows[0]["未打印"]);


            List<string> xData = new List<string>() { "制作完成"+ data.Rows[0]["已打印"].ToString(), "未完成量"+data.Rows[0]["未打印"].ToString() };
            chart2.Titles.Add("制作量数据分析");
            chart2.Titles.Add("证卡总量：" + (int)data.Rows[0]["总量"]);
            chart2.Series[0]["PieLabelStyle"] = "Outside";//将文字移到外侧
            chart2.Series[0]["PieLineColor"] = "Black";//绘制黑色的连线。
            chart2.Series[0].Points.DataBindXY(xData, yData);
           


            chart1.Series[0].Label = "#VAL";
            xData = new List<string>() { "证卡总量", "打印完成","授权完成","发放完成" };
            yData = new List<int>() { (int)data.Rows[0]["总量"], (int)data.Rows[0]["已打印"], (int)data.Rows[0]["已绑卡"], (int)data.Rows[0]["已发卡"] };
            chart1.Titles.Add("全流程数据分析");
            chart1.Titles.Add("证卡总量：" + (int)data.Rows[0]["总量"]);
            chart1.Series[0].Name = "证卡量";
            chart1.Series[0]["PieLabelStyle"] = "Outside";//将文字移到外侧
            chart1.Series[0]["PieLineColor"] = "Black";//绘制黑色的连线。
            chart1.Series[0].Points.DataBindXY(xData, yData);
        }

        private void FrmHome_Load(object sender, EventArgs e)
        {

            CreateChart();
        }

        private void FrmHome_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }
    }
}